package com.software.content_client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btn_call_log;
    private Button btn_insert;
    private ContentResolver cr;
    private Button btn_query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvents();

    }

    private void initEvents() {
        btn_call_log.setOnClickListener(v -> {
            // cp 系统提供
            // cr
            ContentResolver cr = getContentResolver();
            Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    // 获得通话记录数据
                    int nameIndex = cursor.getColumnIndex(CallLog.Calls.CACHED_NAME);
                    String name = cursor.getString(nameIndex);

                    int numberIndex = cursor.getColumnIndex(CallLog.Calls.NUMBER);
                    String number = cursor.getString(numberIndex);

                    Log.i("call-log", "姓名：" + name + ": 电话：" + number);
                } while (cursor.moveToNext());
            }

        });

        btn_insert.setOnClickListener(v -> {
            cr = getContentResolver();
            String uriStr = "content://com.software.content_server.UserInfoContentProvider/user_info";
            Uri uri = Uri.parse(uriStr);
            ContentValues values = new ContentValues();
            values.put("name", "张三");
            values.put("age", "18");
            values.put("height", "180");
            values.put("weight", 45.0F);
            Uri insert = cr.insert(uri, values);
            long row = ContentUris.parseId(insert);
            if (row > 0) {
                // true代表当前路径及其子路径
                // false代表当前路径及其父路径
                UserInfoObserver observer = new UserInfoObserver(null);
                cr.registerContentObserver(uri, true, observer);
                Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
            }
        });

        btn_query.setOnClickListener(v -> {
            cr = getContentResolver();
            String uriStr = "content://com.software.content_server.UserInfoContentProvider/user_info";
            Uri uri = Uri.parse(uriStr);
            Cursor cursor = cr.query(uri, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    int nameIndex = cursor.getColumnIndex("name");
                    String name = cursor.getString(nameIndex);
                    int ageIndex = cursor.getColumnIndex("age");
                    int age = cursor.getInt(ageIndex);
                    int heightIndex = cursor.getColumnIndex("height");
                    int height = cursor.getInt(heightIndex);
                    int weightIndex = cursor.getColumnIndex("weight");
                    float weight = cursor.getFloat(weightIndex);
                    Log.i("user-info", "姓名：" + name + ": 年龄：" + age + ": 高度：" + height + ": 体重：" + weight);
                } while (cursor.moveToNext());
            }
        });
    }

    private void initViews() {

        btn_call_log = findViewById(R.id.btn_call_log);
        btn_insert = findViewById(R.id.btn_insert);
        btn_query = findViewById(R.id.btn_query);
    }
}